package cn.itsource.controller;

import cn.hutool.crypto.SecureUtil;
import cn.itsource.domain.VipUser;
import cn.itsource.dto.RegisterDto;
import cn.itsource.exception.MyException;
import cn.itsource.query.VipUserQuery;
import cn.itsource.result.JSONResult;
import cn.itsource.result.PageList;
import cn.itsource.service.IVipUserService;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @desc
 * @author bobo`
 * @since 2022-11-18
 */
@RestController
@RequestMapping("/vipUser")
public class VipUserController {

    @Autowired
    public IVipUserService vipUserService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 门户网站用户注册接口
     * @return
     */
    @PostMapping("/register")
    public JSONResult register(@RequestBody RegisterDto dto){
        /**
         * 1、入参校验
         * 2、判断用户输入的短信验证码是否正确，先去Redis获取正确值，做判断
         * 3、验证码正确，数据入库
         */

        String smsCodeFromRedis = (String)redisTemplate.opsForValue().get(dto.getMobile());

        if(!StringUtils.hasLength(smsCodeFromRedis)){
            throw new MyException("短信验证码已过期，请重新发送！");
        }

        if(!smsCodeFromRedis.equals(dto.getSmsCode())){
            throw new MyException("短信验证码错误，请重新输入！");
        }

        //数据入库
        VipUser user = new VipUser();
        user.setPhone(dto.getMobile());
        user.setPassword(SecureUtil.md5(dto.getPassword()));
        //.......
        vipUserService.insert(user);
        return JSONResult.success();
    }

    /**
     * 保存和修改操作公用此方法
     * @param vipUser 前端传递来的实体数据
     */
    @PostMapping(value="/save")
    public JSONResult save(@RequestBody VipUser vipUser){
        if(vipUser.getId() != null){
                vipUserService.updateById(vipUser);
        }else{
                vipUserService.insert(vipUser);
        }
        return JSONResult.success();
    }

    /**
    * 根据ID删除指定对象信息
    * @param id
    */
    @DeleteMapping(value="/{id}")
    public JSONResult delete(@PathVariable("id") Long id){
        vipUserService.deleteById(id);
        return JSONResult.success();
    }

    //根据ID查询对象详情信息
    @GetMapping(value = "/{id}")
    public JSONResult get(@PathVariable("id")Long id) {
        return JSONResult.success(vipUserService.selectById(id));
    }


    /**
    * 查看所有对象数据（不分页）
    */
    @GetMapping(value = "/list")
    public JSONResult list(){
        return JSONResult.success(vipUserService.selectList(null));
    }


    /**
    * 分页查询数据
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping(value = "/pagelist")
    public JSONResult pageList(@RequestBody VipUserQuery query) {
        Page<VipUser> page = new Page<VipUser>(query.getPage(),query.getRows());
        page = vipUserService.selectPage(page);
        return JSONResult.success(new PageList<VipUser>(page.getTotal(), page.getRecords()));
    }
}